package bb.lanxing.fragment.sport;

import java.util.ArrayList;

import com.gyf.immersionbar.ImmersionBar;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.activity.sport.SportBeforeActivity;
import bb.lanxing.adapter.sport.SportAdapter;
import bb.lanxing.fragment.base.BaseTabFragment;
import bb.lanxing.lib.devices.api.DeviceManager;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.manager.SharedManager;
import bb.lanxing.model.database.Workout;
import bb.lanxing.util.SportTypeResUtil;
import bb.lanxing.view.BiciAlertDialogBuilder;

public class SportFragment extends BaseTabFragment {
    private final int[] sportType = { Workout.TYPE_CYCLING, Workout.TYPE_RUNNING, Workout.TYPE_WALKING,
            Workout.TYPE_SKIING, Workout.TYPE_SWIMMING,
            // Workout.TYPE_TRAINING, Workout.TYPE_INDOOR_BIKE, Workout.TYPE_BIKE_TRAINER,Workout.TYPE_OTHER};
            Workout.TYPE_TRAINING, Workout.TYPE_INDOOR_BIKE, Workout.TYPE_BIKE_TRAINER, Workout.TYPE_ROWER,
            Workout.TYPE_CROSS_TRAINER, Workout.TYPE_OTHER };
    private RecyclerView recyclerView;

    // private boolean hasBleDeviceConnected() {
    // DeviceManager deviceManager = App.getContext().getDeviceManager();
    // return deviceManager != null && (deviceManager.isConnected(1) || deviceManager.isConnected(2)||
    // deviceManager.isConnected(37) || deviceManager.isConnected(35) || deviceManager.isConnected(36) ||
    // deviceManager.isConnected(19) || deviceManager.isConnected(30) || deviceManager.isConnected(3) ||
    // deviceManager.isConnected(16) || deviceManager.isConnected(6));
    // }

    public SportFragment() {
        super(R.layout.fragment_sport);
    }

    private boolean hasBleDeviceConnected() {
        boolean vRet = false;
        DeviceManager deviceManager = App.getContext().getDeviceManager();
        if (deviceManager != null) {
            if (deviceManager.isConnected(SmartDevice.TYPE_BICI) || deviceManager.isConnected(SmartDevice.TYPE_BES)
                    || deviceManager.isConnected(SmartDevice.TYPE_CADENCE)
                    || deviceManager.isConnected(SmartDevice.TYPE_VORTEX_CADENCE)
                    || deviceManager.isConnected(SmartDevice.TYPE_CADENCE01)
                    || deviceManager.isConnected(SmartDevice.TYPE_HEARTRATE)
                    || deviceManager.isConnected(SmartDevice.TYPE_POWERRATE)
                    || deviceManager.isConnected(SmartDevice.TYPE_BIKE_TRAINER)
                    || deviceManager.isConnected(SmartDevice.TYPE_INDOOR_BIKE)
                    || deviceManager.isConnected(SmartDevice.TYPE_CROSS_TRAINER)
                    || deviceManager.isConnected(SmartDevice.TYPE_ROWER)
                    || deviceManager.isConnected(SmartDevice.TYPE_HEARTRATE_BELT)
                    || deviceManager.isConnected(SmartDevice.TYPE_Q1)
                    || deviceManager.isConnected(SmartDevice.TYPE_HR_X2_PRO)
                    || deviceManager.isConnected(SmartDevice.TYPE_HEART_RATE01)) {
                vRet = true;
            }
        }
        return vRet;
    }

    private boolean hasIndoorBikeConnected() {
        boolean vRet = false;
        DeviceManager deviceManager = App.getContext().getDeviceManager();
        if (deviceManager != null) {
            if (deviceManager.isConnected(SmartDevice.TYPE_INDOOR_BIKE)) {
                vRet = true;
            }
        }
        return vRet;
    }

    private boolean hasBikeTrainerConnected() {
        boolean vRet = false;
        DeviceManager deviceManager = App.getContext().getDeviceManager();
        if (deviceManager != null) {
            if (deviceManager.isConnected(SmartDevice.TYPE_BIKE_TRAINER)) {
                vRet = true;
            }
        }
        return vRet;
    }

    private boolean hasRowerConnected() {
        boolean vRet = false;
        DeviceManager deviceManager = App.getContext().getDeviceManager();
        if (deviceManager != null) {
            if (deviceManager.isConnected(SmartDevice.TYPE_ROWER)) {
                vRet = true;
            }
        }
        return vRet;
    }

    private boolean hasCrossTrainerConnected() {
        boolean vRet = false;
        DeviceManager deviceManager = App.getContext().getDeviceManager();
        if (deviceManager != null) {
            if (deviceManager.isConnected(SmartDevice.TYPE_CROSS_TRAINER)) {
                vRet = true;
            }
        }
        return vRet;
    }

    public void showConnectBleDeviceDialog() {
        new BiciAlertDialogBuilder(getContext()).setTitle(R.string.sport_dialog_connect_ble_title)
                .setMessage(R.string.sport_dialog_connect_ble_content)
                .setNegativeButton(R.string.dialog_btn_ok, (DialogInterface.OnClickListener) null).show();
    }

    public void showConnectIndoorBikeDialog() {
        new BiciAlertDialogBuilder(getContext()).setTitle(R.string.sport_dialog_connect_indoorbike_title)
                .setMessage(R.string.sport_dialog_connect_indoorbike_content)
                .setNegativeButton(R.string.dialog_btn_ok, (DialogInterface.OnClickListener) null).show();
    }

    public void showConnectBikeTrainerDialog() {
        new BiciAlertDialogBuilder(getContext()).setTitle(R.string.sport_dialog_connect_biketrainer_title)
                .setMessage(R.string.sport_dialog_connect_biketrainer_content)
                .setNegativeButton(R.string.dialog_btn_ok, (DialogInterface.OnClickListener) null).show();
    }

    public void showConnectRowerDialog() {
        new BiciAlertDialogBuilder(getContext()).setTitle(R.string.sport_dialog_connect_rower_title)
                .setMessage(R.string.sport_dialog_connect_rower_content)
                .setNegativeButton(R.string.dialog_btn_ok, (DialogInterface.OnClickListener) null).show();
    }

    public void showConnectCrossTrainerDialog() {
        new BiciAlertDialogBuilder(getContext()).setTitle(R.string.sport_dialog_connect_crosstrainer_title)
                .setMessage(R.string.sport_dialog_connect_crosstrainer_content)
                .setNegativeButton(R.string.dialog_btn_ok, (DialogInterface.OnClickListener) null).show();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_sport, container, false);
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        ArrayList<Integer> sportTypeList = new ArrayList<>();
        for (int i = 0; i < sportType.length; i++) {
            sportTypeList.add(sportType[i]);
        }
        SportAdapter sportAdapter = new SportAdapter(sportTypeList);
        sportAdapter.setOnItemClickListener(position -> {
            Intent intent = new Intent(getActivity(), SportBeforeActivity.class);
            intent.putExtra("title", getString(SportTypeResUtil.getTypeTextRes(sportType[position])));
            intent.putExtra("sportType", sportType[position]);
            if (sportType[position] == Workout.TYPE_TRAINING) {
                if (!hasBleDeviceConnected()) {
                    showConnectBleDeviceDialog();
                    return;
                }
            }
            if (sportType[position] == Workout.TYPE_INDOOR_BIKE) {
                if (!hasIndoorBikeConnected()) {
                    showConnectIndoorBikeDialog();
                    return;
                }
            }

            if (sportType[position] == Workout.TYPE_BIKE_TRAINER) {
                if (!hasBikeTrainerConnected()) {
                    showConnectBikeTrainerDialog();
                    return;
                }
            }

            if (sportType[position] == Workout.TYPE_ROWER) {
                if (!hasRowerConnected()) {
                    showConnectRowerDialog();
                    return;
                }
            }

            if (sportType[position] == Workout.TYPE_CROSS_TRAINER) {
                if (!hasCrossTrainerConnected()) {
                    showConnectCrossTrainerDialog();
                    return;
                }
            }
            SharedManager.getInstance().setSportType(sportType[position]);
            startActivity(intent);
        });
        recyclerView.setAdapter(sportAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        ImmersionBar.with(this).transparentStatusBar().statusBarDarkFont(true).init();
    }
}
